package com.rainng.coursesystem.controller;

import com.rainng.coursesystem.repository.PostEntity;
import com.rainng.coursesystem.model.vo.response.PostDetailsVO;
import com.rainng.coursesystem.model.vo.response.PostVO;
import com.rainng.coursesystem.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/posts")
public class PostController {

    @Autowired
    private PostService postService;

    // 提交新帖子
    @PostMapping("/create")
    public ResponseEntity<PostEntity> createPost(@RequestBody @Validated PostEntity post) {
        PostEntity createdPost = postService.createPost(post);
        return ResponseEntity.status(HttpStatus.CREATED).body(createdPost);
    }

    // 删除帖子
    @DeleteMapping("/delete/{postId}")
    public ResponseEntity<String> deletePost(@PathVariable int postId) {
        if (postService.isPostExist(postId)) {
            postService.deletePost(postId);
            return ResponseEntity.ok("Post deleted successfully");
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Post not found");
        }
    }

    // 更新帖子
    @PutMapping("/update/{postId}")
    public ResponseEntity<String> updatePost(@PathVariable int postId, @RequestBody PostEntity post) {
        if (postService.isPostExist(postId)) {
            postService.updatePost(postId, post);
            return ResponseEntity.ok("Post updated successfully");
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Post not found");
        }
    }

    // 点赞帖子
    @PostMapping("/like")
    public ResponseEntity<String> likePost(@RequestParam int userId, @RequestParam int postId) {
        try {
            postService.likePost(userId, postId);
            return ResponseEntity.ok("Post liked successfully");
        } catch (RuntimeException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
        }
    }

    // 取消点赞帖子
    @PostMapping("/unlike")
    public ResponseEntity<String> unlikePost(@RequestParam int userId, @RequestParam int postId) {
        try {
            postService.unlikePost(userId, postId);
            return ResponseEntity.ok("Post unliked successfully");
        } catch (RuntimeException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
        }
    }

    // 搜索帖子和评论
    @GetMapping("/search")
    public ResponseEntity<List<Map<String, Object>>> searchPostsAndComments(
            @RequestParam String keyword,
            @RequestParam int courseId) {
        List<Map<String, Object>> results = postService.searchByKeywordAndCourseId(keyword, courseId);
        return ResponseEntity.ok(results);
    }

    // 收藏帖子
    @PostMapping("/favorite")
    public ResponseEntity<String> favoritePost(@RequestParam int userId, @RequestParam int postId) {
        try {
            postService.favoritePost(userId, postId);
            return ResponseEntity.ok("Post favorited successfully");
        } catch (RuntimeException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
        }
    }

    // 取消收藏帖子
    @PostMapping("/unfavorite")
    public ResponseEntity<String> unfavoritePost(@RequestParam int userId, @RequestParam int postId) {
        try {
            postService.unfavoritePost(userId, postId);
            return ResponseEntity.ok("Post unfavorited successfully");
        } catch (RuntimeException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
        }
    }

    // 获取用户收藏的所有帖子
    @GetMapping("/favorites")
    public ResponseEntity<List<Map<String, Object>>> getFavoritePosts(@RequestParam int userId) {
        List<Map<String, Object>> favoritePosts = postService.getFavoritesWithStatusByUserId(userId);
        return ResponseEntity.ok(favoritePosts);
    }


    // 获取课程下所有帖子及其点赞和收藏状态
    @GetMapping("/postsWithLikes")
    public ResponseEntity<List<Map<String, Object>>> getPostsWithLikes(
            @RequestParam int courseId,
            @RequestParam int userId) {
        List<Map<String, Object>> postsWithStatus = postService.getPostsByCourseAndUser(courseId, userId);
        return ResponseEntity.ok(postsWithStatus);
    }
}
